package com.waner.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waner.reggie.common.R;
import com.waner.reggie.entity.Category;
import com.waner.reggie.entity.Dish;
import com.waner.reggie.entity.Setmeal;
import com.waner.reggie.service.CategoryService;
import com.waner.reggie.service.DishService;
import com.waner.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.wrapper.BaseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * 分类管理
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;  // 用于关联表的查询

    @Autowired
    private SetmealService setmealService; // 用于关联表的查询

    /**
     * 分页获取菜单的数据
     * @param request
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(HttpServletRequest request){
        // 获取路径参数
        int page = Integer.parseInt(request.getParameter("page"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));

        log.info("page{} pageSize{}",page,pageSize);

        // 创建路径查询器
        Page pageInfo = new Page(page,pageSize);

        // 创建条件查询器，使用分类的排序属性进行查询
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByAsc(Category::getSort);

        // 进行查询
        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Category category){
        log.info("新增分类:名称{} 类型{} 序号{}",category.getName(),category.getType(),category.getSort());
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        categoryService.getBaseMapper().insert(category);
        return R.success("添加成功");
    }

    /**
     * 修改菜单
     * @param category
     * @return
     */
    @PutMapping
    public R<String> change(@RequestBody Category category){
        log.info("修改分类{} {} {}",category.getName(),category.getType());

        // 创建查询的语句
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId,category.getId());

        // 使用baseMapper进行更新操作
        categoryService.getBaseMapper().update(category,queryWrapper);

        return R.success("操作成功");
    }

    /**
     * 进行菜品分类的删除操作
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除操作 菜单的id为{}", ids);

        // 调用自定义的删除函数
        categoryService.remove(ids);

        return R.success("操作成功");
    }

    /**
     * 获取分类（菜品或者套餐）
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Integer type){
        // type = type == null ? 1:type;
        // 生成查询器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        queryWrapper.eq(type != null,Category::getType,type);

        // 进行查询
        List<Category> list = categoryService.list(queryWrapper);

        return R.success(list);
    }

}
